﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using DispatchAssist.Model;
using DispatchAssist.BusinessLogical.Model;
using DispatchAssist.Service;

namespace DispatchAssist.AppHost
{
    public partial class frmBubbleAlert : BubbleForm
    {
        private NormalAlertInfo alertinfo = new NormalAlertInfo();

        AlertMessageGetterService service = new AlertMessageGetterService();
        private readonly object locker = new object();
        MeteoMsgAlert mmalertF = new MeteoMsgAlert();
        public frmBubbleAlert()
            : base()
        {
        }

        public frmBubbleAlert(string Content)
            : base(Content)
        {
        }

        public frmBubbleAlert(NormalAlertInfo info, MeteoMsgAlert mmalert)
            : base(info.MessageBody)
        {
            alertinfo = info;
            mmalertF = mmalert;
        }

        protected override void OnShown(EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            service.UpdateAlertStatusCompleted += new UpdateAlertStatusCompletedEventHandler(service_UpdateAlertStatusCompleted);

            base.OnShown(e);
        }

        private void frmBubbleAlert_Load(object sender, EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            service.UpdateAlertStatusCompleted += new UpdateAlertStatusCompletedEventHandler(service_UpdateAlertStatusCompleted);
        }

        void service_UpdateAlertStatusCompleted(object sender, AsyncCompletedEventArgs e)
        {
            lock (locker)
            {
                AlertContext.Dic_Bubble.Remove((string)e.UserState);
                AlertContext.CountJ++;
                AlertContext.Dic_Dispose_Bubble.Remove((string)e.UserState);
            }
        }

        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this.picBg)).BeginInit();
            this.SuspendLayout();
            // 
            // frmBubbleAlert
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.ClientSize = new System.Drawing.Size(228, 155);
            this.DoubleBuffered = true;
            this.Name = "frmBubbleAlert";
            this.Opacity = 1D;
            this.ShowIcon = false;
            this.TransparencyKey = System.Drawing.Color.Empty;
            this.Load += new System.EventHandler(this.frmBubbleAlert_Load);
            ((System.ComponentModel.ISupportInitialize)(this.picBg)).EndInit();
            this.ResumeLayout(false);
        }

        protected override void OnClosed(EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();

            string[] strarr = new string[1];
            strarr[0] = alertinfo.AlertID.ToString();
            if (strarr == null || strarr.Length == 0) return;
            AlertContext.Dic_Dispose_Bubble.Add(strarr[0], this);
            service.UpdateAlertStatusAsync(strarr, (int)(Enums.AlertActionFlag.BubbleAlert), service.DASoapHeaderValue.UserId, strarr[0]);
        }
    }
}
